
**********************************************************************************
* Cleaning File for "When Parents Decide: Gender Differences in Competitiveness" *
* Created by Jonas Tungodden and Alexander Willen				 				 *
* Last Edited July 1, 2022							 							 * 
**********************************************************************************


* Opening the log file and choosing settings
********************************************************************************
log using "/home/alexander/Whenparentsdecide/Cleaning.smcl", replace
version 16
set more off
********************************************************************************

******************************************************************************
* Step 1: Obtaining anonomized person ID ("npid") from the experimental data *
******************************************************************************
{
use "/data/prosjekt/competitive_gender/eksperiment.dta", clear 
keep npid
duplicates drop npid, force
}

*****************************************************************************
* Step 2: Merging parental income in 2016 from the administrative registers *
*****************************************************************************
{
	* Mother
	preserve 
	use "/data/prosjekt/competitive_gender/inntekt_mor.dta", clear
	ren aargang yr
	keep if yr==2016
	duplicates drop npid, force
	save "/home/alexander/Whenparentsdecide/data/temp.dta", replace
	restore

	merge 1:1 npid using "/home/alexander/Whenparentsdecide/data/temp.dta"
	drop if _merge==2
	drop _merge

	* Father
	preserve 
	use "/data/prosjekt/competitive_gender/inntekt_far.dta", clear
	ren aargang yr
	keep if yr==2016
	duplicates drop npid, force
	save "/home/alexander/Whenparentsdecide/data/temp.dta", replace
	restore

	merge 1:1 npid using "/home/alexander/Whenparentsdecide/data/temp.dta"
	drop if _merge==2
	drop _merge

	* Combining and creating SES indicator
	gen parentalincome=pinnt_mor+pinnt_far
	replace parentalincome=pinnt_mor if parentalincome==.		// If only mom, thats the parental income
	replace parentalincome=pinnt_far if parentalincome==.		// If only dad, thats the parental income 
	gen oneparincmissing=1 if pinnt_mor==. | pinnt_far==.		
	sum parentalincome, det
	gen highSES=1 if parentalincome>r(p50) & parentalincome!=.
	replace highSES=0 if parentalincome<r(p50)			
}
	
**************************************
* Step 3: Parental education in 2016 *
**************************************
{
	* Father
	preserve
	use "/data/prosjekt/competitive_gender/utdanning_far.dta", clear
	keep npid bu_2016 
	ren bu_2016 bu_2016_far
	save "/home/alexander/Whenparentsdecide/data/temp.dta", replace
	restore

	merge 1:1 npid using "/home/alexander/Whenparentsdecide/data/temp.dta"
	drop if _merge==2
	drop _merge

	* Mother
	preserve
	use "/data/prosjekt/competitive_gender/utdanning_mor.dta", clear
	keep npid bu_2016 
	ren bu_2016 bu_2016_mor
	save "/home/alexander/Whenparentsdecide/data/temp.dta", replace
	restore

	merge 1:1 npid using "/home/alexander/Whenparentsdecide/data/temp.dta"
	drop if _merge==2
	drop _merge

	* Combining
	destring bu_2016_mor bu_2016_far, force replace 
	gen mcollege  = (bu_2016_mor>500000) if bu_2016_mor<.
	gen fcollege  = (bu_2016_far>500000) if bu_2016_far<.
	gen pcollege  = max(mcollege, fcollege)
}
	
**********************************************
* Step 4: Average grade in compulsory school *
**********************************************
{
preserve
use "/data/prosjekt/competitive_gender/karakter_grunnskule.dta", clear
destring stp, force replace
collapse (mean) stp, by(npid)
ren stp grade10_GPA
save "/home/alexander/Whenparentsdecide/data/temp.dta", replace
restore

merge 1:1 npid using "/home/alexander/Whenparentsdecide/data/temp.dta"
drop if _merge==2
drop _merge
}

************************************
* Education program in high school *
************************************
{
merge 1:1 npid using "/data/prosjekt/competitive_gender/vitnemål.dta"
drop if _merge==2
drop _merge

destring studretn_1, force replace
gen studspec_Y1=studretn_1==62
replace studspec_Y1=. if studretn_1==.

* Not all people that chose ss included in vitnemal, sa adding people back here by imputing using kurskod. 
{
preserve 
use "/data/prosjekt/competitive_gender/karakter_vgs.dta", clear
gen studspec_imp=(fagkode=="SAF1001" | fagkode=="GEO1001")
collapse (max) studspec_imp, by(npid)
save "/home/alexander/Whenparentsdecide/data/temp.dta", replace
restore

drop _merge
merge 1:1 npid using "/home/alexander/Whenparentsdecide/data/temp.dta"
drop if _merge==2
drop _merge
gen sspec_Y1_imp=studspec_Y1
replace sspec_Y1_imp=studspec_imp if sspec_Y1_imp==.
}

save "/home/alexander/Whenparentsdecide/data/admindata", replace
}

*************
* Combining *
************* 
{
use "/data/prosjekt/competitive_gender/eksperiment.dta", clear 
destring , force replace
merge m:1 npid using "/home/alexander/Whenparentsdecide/data/admindata"
duplicates drop npid child, force				// 3 duplicates from data-generating process, eliminating to avoid double counting. 
save "/home/alexander/Whenparentsdecide/data/admindata", replace
}

* END OF DOFILE *
